/*
 * @Date: 2023-02-27 18:18:34
 * @LastEditTime: 2023-02-27 18:25:00
 * @FilePath: /hash-guess-game/src/store/useUserInfoStore.ts
 * 介绍:
 */
import { defineStore } from "pinia";
import { UserInfo, apiUserInfo } from "@@/api/module/userInfo";

export default defineStore("StoreUserInfoStore", {
  //*全局仓库
  state: initState,
  //*全局函数
  actions: {
    async getUserInfo(): Promise<UserInfo.Res | void> {
      const res = await apiUserInfo();
      if (!res) return;
      this.$patch((store) => {
        store.userInfo = res;
      });
      return res;
    },
  },
  //*计算属性
  getters: {},
  //*仓库数据缓存
  persist: {
    enabled: true,
    strategies: [
      {
        key: "StoreUserInfoStore",
        storage: localStorage, //指定储存位置
      },
    ],
  },
});
/**state类型定义 */
interface StoreUserInfoStore {
  userInfo: UserInfo.Res | undefined;
}
/**初始化pinia */
function initState(): StoreUserInfoStore {
  return {
    userInfo: undefined,
  };
}
